package com.mindskip.zdw.configuration.spring.mvc;

import com.mindskip.zdw.configuration.property.SystemConfig;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;


@Configuration
public class WebMvcConfiguration extends WebMvcConfigurationSupport {

//  private final TokenHandlerInterceptor tokenHandlerInterceptor;
   private final SystemConfig systemConfig;

//    /**
//     * Instantiates a new Web mvc configuration.
//     *
//     * @param tokenHandlerInterceptor the token handler interceptor
//     * @param systemConfig            the system config
//     */
    @Autowired
    public WebMvcConfiguration( SystemConfig systemConfig) {

        this.systemConfig = systemConfig;
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addRedirectViewController("/", "/student/index.html");
        registry.addRedirectViewController("/student", "/student/index.html");
        registry.addRedirectViewController("/admin", "/admin/index.html");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/static/")
                .setCachePeriod(31556926);
    }

//    @Override
//    public void addInterceptors(InterceptorRegistry registry) {
//        List<String> securityIgnoreUrls = systemConfig.getWx().getSecurityIgnoreUrls();
//        String[] ignores = new String[securityIgnoreUrls.size()];
//        registry.addInterceptor(tokenHandlerInterceptor)
//                .addPathPatterns("/api/wx/**")
//                .excludePathPatterns(securityIgnoreUrls.toArray(ignores));
//        super.addInterceptors(registry);
//    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowCredentials(true)
                .allowedMethods("*")
                .allowedOrigins("*")
                .allowedHeaders("*");
        super.addCorsMappings(registry);
    }

}
